package com.wang.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wang.admin.api.client.AdChannelLabelFeign;
import com.wang.admin.api.client.AdLabelFeign;
import com.wang.common.model.dto.PageResponseResult;
import com.wang.common.model.dto.ResponseResult;
import com.wang.common.model.enums.AppHttpCodeEnum;
import com.wang.wemedia.api.model.dto.WmChannelPageDTO;
import com.wang.wemedia.api.model.pojo.WmChannel;
import com.wang.wemedia.mapper.WmChannelMapper;
import com.wang.wemedia.service.WmChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 频道信息逻辑处理
 *
 * @author: wangning
 * @date: 2024/1/12 10:23
 */
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    private final WmChannelMapper wmChannelMapper;
    private final AdLabelFeign adLabelFeign;
    private final AdChannelLabelFeign adChannelLabelFeign;

    /**
     * 新增频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult add(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmChannel::getName, wmChannel.getName());
        // 唯一校验
        if (Objects.nonNull(wmChannelMapper.selectOne(wrapper))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // insert -> wmChannel
        wmChannel.setCreatedTime(new Date());
        return ResponseResult.okResult(wmChannelMapper.insert(wmChannel));
//        // insert -> adLabel
//        AdLabel adLabel = new AdLabel();
//        BeanUtils.copyProperties(wmChannel, adLabel);
//        adLabel.setType((short) 1);
//        Integer adLabelId = adLabelFeign.add(adLabel);
//        // insert -> adChannelLabel
//        AdChannelLabel adChannelLabel = new AdChannelLabel();
//        BeanUtils.copyProperties(wmChannel, adChannelLabel);
//        adChannelLabel.setLabelId(adLabelId);
//        adChannelLabel.setChannelId(adChannelLabel.getId());
//        adChannelLabelFeign.add(adChannelLabel);
//        return ResponseResult.okResult("新增成功");
    }

    @Override
    public ResponseResult delete(Integer id) {
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmChannel::getId, id);
        WmChannel wmChannel = wmChannelMapper.selectOne(wrapper);
        // 程序健壮性，代码非空判断
        if (Objects.isNull(wmChannel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(301, "只能删除禁用的频道");
        }
        return ResponseResult.okResult(wmChannelMapper.deleteById(id));
    }

    @Override
    public ResponseResult alter(WmChannel wmChannel) {
        return ResponseResult.okResult(wmChannelMapper.updateById(wmChannel));
    }

    /**
     * 查询所有频道
     *
     * @return List<WmChannel>
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询频道列表
     *
     * @param wmChannelPageDTO
     * @return
     */
    @Override
    public ResponseResult listWmChannel(WmChannelPageDTO wmChannelPageDTO) {
        Integer size = wmChannelPageDTO.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        PageHelper.startPage(wmChannelPageDTO.getPage(), size);
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.like(WmChannel::getName, wmChannelPageDTO.getName());
        List<WmChannel> wmChannelList = wmChannelMapper.selectList(wrapper);
        PageInfo<WmChannel> pageInfo = new PageInfo<>(wmChannelList);
        PageResponseResult pageResult =
                new PageResponseResult(pageInfo.getPageNum(), pageInfo.getSize(), (int) pageInfo.getTotal());
        return pageResult.okResult(pageInfo.getList());
    }

}
